<?php

class ArticleController extends Controller
{
	public function actionIndex()
	{
	  $articles = Article::model()->getArticles(1,'','true');
		$this->render('index', array(
		  'articles' => $articles
		));
	}
	
  public function actionBlog()
	{
	  $articles = Article::model()->getArticles(1,'','');
		$this->render('index', array(
		  'articles' => $articles
		));
	}
  
	public function actionPromotion()
	{
	  $articles = Article::model()->getArticles(2,'','');
		$this->render('index', array(
		  'articles' => $articles
		));
	}
	
	public function actionSample()
	{
	  $articles = Article::model()->getArticles(3,'','');
		$this->render('index', array(
		  'articles' => $articles
		));
	}
	
   public function actionDetail($id)
	{
	  $model = $this->loadModel($id);
	  $criteria = Article::model()->search()->criteria;
	  $criteria->addCondition("t.id = $model->id");
	  $article = Article::model()->find($criteria);
      
    $related_articles = Article::model()->getArticles($model->id);
    
		$this->render('detail', array(
		  'article' => $article,		 
		  'related_articles' => $related_articles
		));
	}
	
	
	public function loadModel($id)
	{
	  $model = Article::model()->find(array(
	    "condition" => "md5(`id`) = '$id' AND enabled = 1"
	  ));
	  
	  if ($model === null)
	    throw new CHttpException(404,'The requested page does not exist.');
    return $model;
	}
}
